﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Controller.ShakeGestures;
using Ads;
using System.Xml.Linq;
using System.IO;
using System.Windows.Threading;
using Microsoft.Phone.Tasks;

namespace OmnipotentFlashlight
{
    public partial class MainPage : PhoneApplicationPage
    {
        bool loaded = false;
        public List<AdsModel> adss = new List<AdsModel>();
        DispatcherTimer adTimer = null;
        int AdNo = -1;
        // 构造函数
        public MainPage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
            if (Common.IsHtcMozart)
            {
                App.isMozart = true;
            }
        }

        //初始化
        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            string start = Common.GetSetting("default_start");
            initSetting();
            if (!loaded)
            {
                if (start == "led")
                    NavigationService.Navigate(new Uri("/LED.xaml?action=light", UriKind.Relative));
                else if (start == "sos")
                    NavigationService.Navigate(new Uri("/screen.xaml?action=sos", UriKind.Relative));
                else if (start == "screen")
                    NavigationService.Navigate(new Uri("/screen.xaml?action=screen", UriKind.Relative));
                else if (start == "change")
                    NavigationService.Navigate(new Uri("/screen_change.xaml", UriKind.Relative));
                loaded = true;
            }

            if (App.isListBtnADS == null)
            {
                ShowAds(); //初始化广告
            }

        }

        //初始化设置
        private void initSetting()
        {
            string start = Common.GetSetting("default_start");
            if (start == "led")
                Pik_Default.SelectedIndex = 1;
            else if (start == "sos")
                Pik_Default.SelectedIndex = 2;
            else if (start == "screen")
                Pik_Default.SelectedIndex = 3;
            else if (start == "change")
                Pik_Default.SelectedIndex = 4;
            else Pik_Default.SelectedIndex = 0;

            string screen_light = Common.GetSetting("screen_light");
            if (screen_light == "gao")
                Pik_ScreenLight.SelectedIndex = 0;
            else if (screen_light == "zhong")
                Pik_ScreenLight.SelectedIndex = 1;
            else if (screen_light == "di")
                Pik_ScreenLight.SelectedIndex = 2;

            string screen_shake = Common.GetSetting("screen_shake");
            if (screen_shake == "guanbi")
                Pik_ScreenShake.SelectedIndex = 0;
            else if (screen_shake == "gao")
                Pik_ScreenShake.SelectedIndex = 1;
            else if (screen_shake == "zhong")
                Pik_ScreenShake.SelectedIndex = 2;
            else if (screen_shake == "di")
                Pik_ScreenShake.SelectedIndex = 3;

            string change_mode = Common.GetSetting("change_mode");
            if (change_mode == "bianse")
                Pik_ChangeMode.SelectedIndex = 0;
            else if (change_mode == "shanshuo")
                Pik_ChangeMode.SelectedIndex = 1;
            else if (change_mode == "jingdeng")
                Pik_ChangeMode.SelectedIndex = 2;
            else if (change_mode == "caihong")
                Pik_ChangeMode.SelectedIndex = 3;
        }

        #region 广告设置

        private void ShowAds()
        {
            App.ads.OnInitRequest += () =>
            {
                App.ads.pos = "1";
                App.isListBtnADS = true;
                Btn_Soft.Visibility = Visibility.Visible;
            };
            App.isListBtnADS = false;
            App.ads.AdsInit();

            App.ads.OnGetAdsRequest += new AdsHelper.GetAdsRequest(ads_OnGetIndexRequest);
            App.ads.GetAds(1, 1, 0);
        }

        void ads_OnGetIndexRequest(AdsModels ads)
        {
            if (ads == null || ads.list == null ||
                ads.list.Count <= 0 || ads.isnextpage == null
                || ads.isnextpage.Length == 0) return;
            adss = ads.list;
            adTimer = new DispatcherTimer();
            adTimer.Interval = TimeSpan.FromMilliseconds(5000);
            adTimer.Tick += (s, e) =>
            {
                if (AdNo == ads.list.Count - 1) AdNo = -1;
                AdNo++;
                BtnAds.Tag = ads.list[AdNo].imgUrl;
            };
            canAds.Visibility = Visibility.Visible;
            adTimer.Start();
            App.ads.OnGetAdsRequest -= ads_OnGetIndexRequest;
        }

        protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
        {
            if (adTimer != null) { adTimer.Stop(); }
            App.ads.OnGetAdsRequest -= ads_OnGetIndexRequest;
            base.OnNavigatingFrom(e);
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            if (App.isListBtnADS == true && adTimer != null)
            {
                adTimer.Start();
            }
            base.OnNavigatedTo(e);
        }

        private void BtnAds_Click(object sender, RoutedEventArgs e)
        {
            new WebBrowserTask() { Uri = new Uri(adss[AdNo].url, UriKind.Absolute) }.Show();
        }

        #endregion

        #region 手电筒 按钮事件

        private void Btn_LED_Click(object sender, RoutedEventArgs e)
        {
            if (App.isMozart) { MessageBox.Show("你的手机莫扎特因为不是LED灯，故无法使用手电筒的功能。"); return; }
            NavigationService.Navigate(new Uri("/LED.xaml?action=light", UriKind.Relative));
        }

        private void Btn_SOS_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/screen.xaml?action=sos", UriKind.Relative));
        }

        private void Btn_Screen_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/screen.xaml?action=screen", UriKind.Relative));
        }

        private void Btn_ScreenChange_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/screen_change.xaml", UriKind.Relative));
        }

        #endregion

        #region 设置 选择事件

        private void Pik_Default_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (loaded)
                Common.SetSetting("default_start", ((sender as ListPicker).SelectedItem as ListPickerItem).Tag.ToString());
        }

        private void ListPicker_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (loaded)
                Common.SetSetting("screen_light", ((sender as ListPicker).SelectedItem as ListPickerItem).Tag.ToString());
        }

        private void ListPicker_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            if (loaded)
                Common.SetSetting("screen_shake", ((sender as ListPicker).SelectedItem as ListPickerItem).Tag.ToString());


        }

        private void ListPicker_SelectionChanged_2(object sender, SelectionChangedEventArgs e)
        {
            if (loaded)
                Common.SetSetting("change_mode", ((sender as ListPicker).SelectedItem as ListPickerItem).Tag.ToString());
        }


        #endregion

        #region 其他 控件事件

        private void Btn_Soft_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Soft.xaml", UriKind.Relative));
        }

        private void Btn_Back_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/BackServer.xaml", UriKind.Relative));
        }

        private void Btn_About_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/About.xaml", UriKind.Relative));
        }

        #endregion





    }
}